using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using System.IO;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Bll;
using System.Collections.Generic;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class PhotoList : AdminBaseUserControl
    {
        public event EventHandler EditPhoto;

        public string SelectedID
        {
            get { return Convert.ToString(ViewState["PhotoID"]); }
            set { ViewState["PhotoID"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlView.Items.Clear();
                ddlView.Items.Add(new ListItem("List view","0"));
                ddlView.Items.Add(new ListItem("Thumbnails", "1"));
            }

        }

        public void DoBindPhoto(object sender, EventArgs e)
        {
            CategoryTree ucCategoryTree = (CategoryTree ) sender;
            long categoryID = Convert.ToInt64(ucCategoryTree.SelectedValue);
            BindPhoto(categoryID);
        }

        private void BindPhoto(long categoryID)
        {
            IContentCategory category = CategoryController.GetCategoryNode(categoryID);
            gvList.DataSource = category.CategoryList;
            gvList.DataBind();
            dlThubnail.DataSource = category.CategoryList;
            dlThubnail.DataBind();
            ViewState["CategoryID"] = categoryID;
            Session["CategoryID"]=  categoryID;
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            long photoID = Convert.ToInt64(e.CommandArgument);

            if (e.CommandName == "delPhoto")
            {
                CategoryController.DeleteCategory(photoID);
                string path = Path.Combine(DataRootPhysicalPath, "Photos/" + photoID + ".jpg");
                FileInfo file = new FileInfo(path);
                file.Delete();
                BindPhoto(Convert.ToInt64(Session["CategoryID"]));
                
            }

            if (e.CommandName == "editPhoto")
            {
                ViewState["PhotoID"] = photoID;
                EditPhoto(this, new EventArgs());

            }

        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                IContentCategory category= (IContentCategory) e.Row.DataItem;
                Image imgPhoto= (Image) e.Row.FindControl("imgPhoto");
                imgPhoto.ImageUrl = "../../ImageView.aspx?photo=" + category.ID + ".jpg&mdim=50";
            }

        }

        protected void dlThubnail_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                IContentCategory category = (IContentCategory) e.Item.DataItem;
                Image imgPhoto = (Image)e.Item.FindControl("imgPhoto");
                 imgPhoto.ImageUrl = "../../ImageView.aspx?photo=" + category.ID + ".jpg&mdim=100";
            }
        }

        protected void dlThubnail_ItemCommand(object source, DataListCommandEventArgs e)
        {
            long photoID = Convert.ToInt64(e.CommandArgument);

            if (e.CommandName == "delPhoto")
            {
                CategoryController.DeleteCategory(photoID);
                try
                {
                    string path = Path.Combine(DataRootPhysicalPath, "Photos/" + photoID + ".jpg");
                    FileInfo file = new FileInfo(path);
                    file.Delete();
                }
                catch
                {
                }

                BindPhoto(Convert.ToInt64(Session["CategoryID"] ));


            }

            if (e.CommandName == "editPhoto")
            {
                ViewState["PhotoID"] = photoID;

                EditPhoto(this, new EventArgs());

            }

        }

        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;

        }

        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;
            BindPhoto(Convert.ToInt64(Session["CategoryID"]));
        }

        protected void ddlView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlView.SelectedValue == "0")
            {
                pnlList.Visible = true;
                pnlThumbnail.Visible = false;
            }
            else
            {
                pnlList.Visible = false;
                pnlThumbnail.Visible = true;
            }

        }
    }
}